//
//  MKJChatTableViewCell.m
//  纯代码气泡聊天
//
//  Created by 宓珂璟 on 16/6/3.
//  Copyright © 2016年 宓珂璟. All rights reserved.
//

#import "MKJChatTableViewCell.h"
#import "UIImage+Layout.h"
#import "FullScreenImageBrowserView.h"
#import "AppDelegate.h"

#define AVATAR_WIDTH 40.0
#define AVATAR_HEIGHT 40.0

@implementation MKJChatTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor=[UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        //发送时间标签
        self.send_time_bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, SENDTIME_H)];
        self.send_time_bg.layer.cornerRadius = 4;
        self.send_time_bg.backgroundColor = COLOR(180, 180, 180);
        self.send_time = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, SENDTIME_H)];
        self.send_time.font = [UIFont systemFontOfSize:14.0f];
        self.send_time.textAlignment = NSTextAlignmentCenter;
        self.send_time.textColor = WHITE_COLOR;
        [self.send_time_bg addSubview:self.send_time];
        [self.contentView addSubview:self.send_time_bg];
        //头像
        self.headImageView = [[UIImageView alloc] init];
        self.headImageView.layer.cornerRadius = AVATAR_WIDTH/2;
        self.headImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.headImageView];
        //内容
        self.backView = [[UIImageView alloc] init];
        [self.backView setUserInteractionEnabled:YES];
        [self.contentView addSubview:self.backView];
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:17.0f];
        [self.backView addSubview:self.contentLabel];
    }
    return self;
}

- (void)refreshCell:(MKJChatModel *)model
{
    CGFloat base_y = [self renderSendtime:model];
    
    // 首先计算文本宽度和高度
    CGRect rec = [model.content boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    // 气泡
    UIImage *bubble = nil;
    // 模拟左边
    if (!model.isRight)
    {
        self.headImageView.frame = CGRectMake(10, base_y + 6, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(60, base_y + 8, rec.size.width + 40, rec.size.height + 20);
        bubble = [UIImage imageNamed:@"对话气泡2"];
    }
    else // 模拟右边
    {
        self.headImageView.frame = CGRectMake(SCREEN_WIDTH - AVATAR_WIDTH - 10, base_y + 7, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(SCREEN_WIDTH - AVATAR_WIDTH - 25 - rec.size.width - 40, base_y + 6, rec.size.width + 40, rec.size.height + 20);
        bubble = [UIImage imageNamed:@"对话气泡1"];
    }
    //设置九宫格拉伸点
    bubble = [bubble resizableImageWithCapInsets:UIEdgeInsetsMake(25,30,25,30)];
    
    self.backView.image = bubble;
    if (!model.isRight) {
        self.headImageView.image = [UIImage imageNamed:@"专家"];
    }
    else {
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
    }
    
    // 文本内容的frame
    self.contentLabel.frame = CGRectMake(model.isRight ? 15 : 23, 8, rec.size.width, rec.size.height);
    if (model.isRight) {
        self.contentLabel.textColor = WHITE_COLOR;
    }
    self.contentLabel.text = model.content;
}

- (void)photoCell:(MKJChatModel *)model
{
    [self.contentLabel setHidden:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(OnTapImage)];
    [self.backView addGestureRecognizer:tap];
    
    CGFloat base_y = [self renderSendtime:model];
    // 头像
    UIImage *headImage = nil;
    // 模拟左边
    if (!model.isRight)
    {
        // 当输入只有一个行的时候高度就是20多一点
        self.headImageView.frame = CGRectMake(10, base_y + 8, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(AVATAR_WIDTH + 20, base_y + 10, 120, 120);
        headImage = [UIImage imageNamed:@"专家"];
        
        UIImage *bubble = [UIImage imageNamed:@"对话气泡2"];
        UIImage *new_bubble = [UIImage getInsetsScaleImageFrom:bubble toSize:self.backView.frame.size];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = self.backView.bounds;
        layer.contents = (id)new_bubble.CGImage;
        layer.contentsScale = [UIScreen mainScreen].scale;
        self.backView.layer.mask = layer;
    }
    else // 模拟右边
    {
        self.headImageView.frame = CGRectMake(SCREEN_WIDTH - AVATAR_WIDTH - 10, base_y + 10, AVATAR_WIDTH, AVATAR_HEIGHT);
        self.backView.frame = CGRectMake(SCREEN_WIDTH - AVATAR_WIDTH - 25 - 120, base_y + 10, 120, 120);
        
        UIImage *bubble = [UIImage imageNamed:@"对话气泡1"];
        UIImage *new_bubble = [UIImage getInsetsScaleImageFrom:bubble toSize:self.backView.frame.size];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = self.backView.bounds;
        layer.contents = (id)[new_bubble CGImage];
        layer.contentsScale = [UIScreen mainScreen].scale;
        self.backView.layer.mask = layer;
    }
    NSString *img_url = model.imageurls;
    if ([[img_url substringFromIndex:img_url.length-1] compare:@","]==0) {
        img_url = [img_url substringToIndex:img_url.length - 1];
    }
    if (model.images.count>0) {
        [self.backView setImage:model.images[0]];
    }
    else {
        [self.backView sd_setImageWithURL:[NSURL URLWithString:img_url]];
    }
    
    if (!model.isRight) {
        self.headImageView.image = headImage;
    }
    else {
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.dSender.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
    }
}

-(NSUInteger)renderSendtime:(MKJChatModel*)model{
    NSUInteger base_y = 0;
    if (model.isShowTime) {
        CGRect tframe = self.send_time_bg.frame;
        tframe.size.width = 140;
        tframe.origin.x = (SCREEN_WIDTH - tframe.size.width)/2;
        [self.send_time_bg setFrame:tframe];
        [self.send_time setFrame:CGRectMake(0, 0, tframe.size.width, tframe.size.height)];
        self.send_time.text = model.createdDate;
        base_y = tframe.size.height;
    }
    else {
        [self.send_time_bg setHidden:YES];
    }
    
    return base_y;
}

-(void)OnTapImage{
    FullScreenImageBrowserView *v = [[FullScreenImageBrowserView alloc]initWithImage:self.backView.image];
    AppDelegate *app=(AppDelegate*)[UIApplication sharedApplication].delegate;
    [app.window addSubview:v];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
